'use strict';
const fs = require("fs")
const Controller = require('egg').Controller;

class Morehouse extends Controller {
  async Morehouse() {

    let page = this.ctx.query
    let area = page.areas // 获取地区
    let pepnums = page.pepnums
    let pages = page.page
    let isfree = page.isfree
    let lowpri = page.lowprice // 获取最低价
    let highpri = page.highprice // 获取最高价
    let from
    let to = 5
    let sql
    let sql2 = `SELECT hid FROM house`
    if (area == undefined) area = '东城'
    if (pepnums == undefined) {
      pepnums = 0
    }
    // 限制人数
    if (pepnums > 5) {
      pepnums = 0
    }
    // 设置isfree
    if (isfree == undefined) {
      isfree = 1
    }
    // 设置价格默认值
    if (lowpri == undefined) lowpri = 0
    if (highpri == undefined) highpri = 5000
    // console.log(lowpri, highpri, 8888);

    let numarr = `SELECT * FROM house WHERE peonum = ${pepnums} AND isfree=${isfree} AND tedian="${area}" AND (price BETWEEN ${lowpri} AND ${highpri})`
    let numsql = await this.ctx.app.mysql.query(numarr)
    // console.log(numsql, 6666666666);
    if (pepnums > 0) {
      // console.log(pepnums);
      this.ctx.body = {
        data: numsql,
        total: numsql.length,
        from: [0, numsql.length]
      }

    } else {

      if (pages == 1) {
        from = 1
      }
      if (pages == 2) {
        from = 6
      }
      if (pages == 3) {
        from = 11
      }
      if (pages == 4) {
        from = 16
      }
      if (pages == 5) {
        from = 21
      }
      sql = `SELECT * FROM house WHERE tedian="${area}" AND isfree=${isfree} AND (price BETWEEN ${lowpri} AND ${highpri}) LIMIT ${from},${to}`
      sql2 = `SELECT * FROM house WHERE tedian="${area}" and isfree=${isfree} AND (price BETWEEN ${lowpri} AND ${highpri})`


      let arr = await this.ctx.app.mysql.query(sql)
      let arr2 = await this.ctx.app.mysql.query(sql2)
      console.log(arr);
      arr2 = arr2.length - 1
      console.log(arr2);
      this.ctx.body = {
        data: arr,
        total: arr2,
        from: [from, to]
      }
    }
  }

  async closepage() {
    let req = this.ctx.query.pepnums
    let isfree = this.ctx.query.isfree
    if (isfree == undefined) isfree = 1
    if (req == undefined) {
      req = 0
    }
    if (req != 0) {
      let numarr = `select  * from house where peonum = ${req} and isfree=${isfree}`
      let numsql = await this.ctx.app.mysql.query(numarr)
      this.ctx.body = {
        data: numsql,
        total: numsql.length,
      }
    } else {
      let sql3 = `SELECT * FROM house WHERE isfree=${isfree}`
      let arr3 = await this.ctx.app.mysql.query(sql3)
      this.ctx.body = {
        total: arr3.length,
        data: arr3
      }
    }

  }

  async map() {
    let hid = this.ctx.query.hid
    let sql = `SELECT * FROM map where mhid=${hid}`
    let arr = await this.ctx.app.mysql.query(sql)
    this.ctx.body = {
      arr
    }
  }
  async city() {
    let sql1 = `SELECT * FROM city`
    let arr1 = await this.ctx.app.mysql.query(sql1)
    this.ctx.body = {
      city: arr1
    }
  }
}

module.exports = Morehouse;